/**
 * Copyright (c) 快宝网络 kuaidihelp.com Co., Ltd. All Rights Reserved 禁止外泄以及用于其它的商业用途
 */

import React, { useRef } from 'react';
import styles from './index.less';
import classNames from 'classnames';
import { isUndefined } from 'lodash';
import { useObserverBoundingClientRect } from '@base/hooks';

interface AffixExtendProps {
  height?: number;
  backgroundColor?: string | 'transparent';
  offsetTop?: number;
  offsetBottom?: number;
  zIndex?: number;
  className?: string;
}

const AffixExtend: React.FC<AffixExtendProps> = (props) => {
  const ref = useRef<HTMLDivElement>();

  const { height: bHeight, parentTop: bTop = 0 } = useObserverBoundingClientRect(ref);

  const {
    height = bHeight,
    offsetTop = bTop,
    backgroundColor,
    offsetBottom,
    zIndex,
    className,
  } = props;

  const rootStyle = { height };

  const contentStyle = {
    backgroundColor,
    ...(isUndefined(offsetBottom)
      ? { top: offsetTop > 0 ? offsetTop : 0 }
      : { bottom: offsetBottom }),
    zIndex,
  };

  const contentCls = classNames(styles.affix, className);

  return (
    <div className={styles.affixWrap} style={rootStyle}>
      <div className={contentCls} style={contentStyle} ref={ref as React.LegacyRef<HTMLDivElement>}>
        {props.children}
      </div>
    </div>
  );
};

export default AffixExtend;
